<?php
class userController extends baseController{
    protected $appID = 'user';
    
    public function __construct(){
        parent::__construct();
        $this->appID = empty($appID) ? $this->appID : $appID;
        $this->appID = $this->appConfig['COOKIE_PREFIX'].$this->appID;
        $config=appConfig('member');
        $this->user_config=$config['APP_SETTING'];
        $this->checkLogin();
    }

    //模板显示含公共
    protected function show($tpl = ''){
        //公共赋值
        $hook_list=ApiHook('hook_user_tpl_assign');
        if(!empty($hook_list)){
            foreach ($hook_list as $app_name => $value) {
                $this->view()->assign($value);
            }
        }
        $content=$this->display($tpl, true, true);
        $layout=$this->display('common', true, true,'member');
        echo str_replace('<!--common-->',$content,$layout);
    }

    protected function checkLogin(){
        //不需要登录验证的页面
        $noLogin = api(APP_NAME,'hook_user_login_exclude');
        //如果当前访问是无需登录验证，则直接返回       
        if( isset($noLogin[CONTROLLER_NAME]) && in_array(ACTION_NAME, $noLogin[CONTROLLER_NAME]) ){
            return true;
        }
        //没有登录,则跳转到登录页面
        if(!$this->isLogin()){
            $this->redirect( url('login/index') );
        }
        //扩展接口
        ApiHook('hook_user_check_login');
        return true;
    }

    //获取登录
    protected function getLogin(){
        return $this->user_id;
    }
    
    //判断是否登录
    protected function isLogin(){
        $userid=intval(get_cookie($this->appID . '_uid'));
        if(empty($userid)){
            return false;
        }else{
            $this->user_id = $userid;
            return true;
        }
    }
    
    //设置登录
    protected function setLogin( $userInfo ,$time = 0){
        return set_cookie($this->appID . '_uid' ,$userInfo, '/' ,$time);
    }


    protected function errorpage($mag){
        header('HTTP/1.1 404 Not Found');
        header('Status: 404 Not Found');
        echo $mag;
        exit;
    }

    //判断空值
    public function alert_str($srt,$type=0,$json=false)
    {
        switch ($type) {
            case 'int':
                $srt=intval($srt);
                break;
            default:
                $srt=in($srt);
                break;
        }
        if(empty($srt)){
            if($json){
                $this->msg('内部通讯错误！',0);
            }else{
                $this->error('内部通讯错误！');
            }
        }
    }

   

}